﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int val = 0, ListNode next = null)
        {
            this.val = val;
            this.next = next;
        }
    }
    class SolKList
    {
        public ListNode ReverKList(ListNode head, int k)
        {

            ListNode end = head;
            for (int i = 0; i < k; i++)
            {
                if (end != null)
                {
                    end = end.next;
                }
                else
                {
                    return head;
                }
            }

            ListNode newHead = ReverseList(head, end);
            head.next = ReverKList(end, k);
            return newHead;



        }



        ListNode ReverseList(ListNode head, ListNode end)
        {
            ListNode dummyHead = new ListNode();



            while (head!= end)
            {
                ListNode next = head.next;
                head.next = dummyHead.next;
                dummyHead.next = head;
                head = next;

            }
            return dummyHead.next;

        }
    }
}

